<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Order extends Backend_Controller {

	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();	
		//加载验证类
		$this->load->library('form_validation');
	}

	/**
	 * 显示所有订单
	 */
	public function index() {
		$this->isValidRequest();	
		$this->load->model('backend/Order_model', 'Order');
		$result = $this->Order->getOrderAll($this->page, $this->input->post());
		$this->load->view('backend/order/index', $result);
	}
	
	public function refund() {
		$this->isValidRequest();	
		$this->load->model('backend/Refund_model', 'Refund');
		$result = $this->Refund->getRefundAll($this->page, $this->input->post());
		$this->load->view('backend/order/refund', $result);
	}
	
	public function multiRefund() {
		$this->isValidRequest();	
		set_time_limit(0);
		$ids = $this->input->post_get('ids');
		$this->load->model('api/Order_model', 'Order');
		$this->load->model('backend/Refund_model', 'Refund');
		$this->load->library('JPush');
		$refund = $this->Refund->multiRefund($ids);
		$wxpayOrder = $wxpayErr = 0;
		$alipayOrder = 0;
		$config = array();
		if(!empty($refund)) {
			$alipay = array();
			foreach ($refund as $v) {
				switch($v['pay_method']) {
					case REST_Controller::PAY_METHOD_ALIPY:
						$alipay['ids'][] = $v['id'];
						$alipay['data'][] = "{$v['ext_order_num']}^{$v['netpay']}^协商退款";
						break;
					case REST_Controller::PAY_METHOD_WECHAT:
						$this->load->library('wxpay');
						/* 选择一个随机的方案 */
						mt_srand((double) microtime() * 1000000);
						$v['refund_num'] = '2' . str_pad($v['address_id'], 4, '0', STR_PAD_LEFT) . date('ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
						$v['op_user_id'] = $this->identity['id'];
						$result = $this->wxpay->refund($v);
						if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
							$data = array();
							$time = time();
							$data['id'] = $v['id'];
							$data['status'] = REST_Controller::STATUS_DELETE;
							$data['refund_num'] = $v['refund_num'];
							$data['ext_refund_num'] = $result['refund_id'];
							$data['updated_at'] = $time;
							$data['refund_time'] = $time;
							$data['op_user_id'] = $this->identity['uid'];

							$data1 = array();
							$data1['id'] = $v['oid'];
							$data1['status'] = REST_Controller::PAY_STATUS_REFUNDED;
							$data1['updated_at'] = $time;
							$opResult = $this->Refund->refund($data, $data1);
							if($opResult) {
								$wxpayOrder++;
								$this->Order->log($v['oid'], $data ['status'], '', 3);
								$this->jpush->push(array($v['user_id']), 'cfg_user_notify_refund', $v, array('data' => $v['oid']));
							}
						} else {
							$wxpayErr++;
						}
						break;
					default:break;
				}
			}
			if(!empty($alipay)) {
				$alipayOrder=count($alipay['data']);
				$data = array();
				mt_srand((double) microtime() * 1000000);
				$data['batch_no'] = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
				$data['batch_num'] = count($alipay['data']);
				$data['detail_data'] = implode('#', $alipay['data']);
				$this->load->library('Alipay');
				$html = $this->alipay->refund($data);
				if($html) {
					$this->Refund->update(array('refund_num'=>$data['batch_no']), array('id'=>$alipay['ids']));
					$config['dataCallback'] = base64_encode(urlencode($html));
					$config['navTabId'] = 'backendorderindex';
				}
			}
			$this->renderJson(200, "wxpay:{$wxpayOrder},wxpay fail:{$wxpayErr}, alipay:{$alipayOrder}", $config);
		}
		
		$this->renderJson(300);
	}
	public function alipay() {
		$html = $this->input->get('form');
		if(!empty($html)) {
			$data['html'] = urldecode(base64_decode($html));
			$this->load->view('backend/order/alipay', $data);
		}
	}
}